var definition = {
	
	/**
	 * All definitions that are found
	 */
	items: [],

	/**
	 * Do a ajax request to get the definition.
	 */
	getDefinition: function(){
		ajax("http://glosbe.com/gapi/translate?from=nl&dest=nl&format=json&phrase=" + Woordenschat.word.toLowerCase(), function(lowercase){
			if(lowercase.tuc == undefined) {
				ajax("http://glosbe.com/gapi/translate?from=nl&dest=nl&format=json&phrase=" + Woordenschat.word, function(normalcase){
					definition.procesDefinition(normalcase);
				});
				} else {
				definition.procesDefinition(lowercase);
			}
		});
	},
	
	/**
	* Extracting 3 definitions from the Json results and putting it in the array 'items'.
	* @param Object definitionList							The Json result from the function 'getDefinition' either searched in lowercase or normalcase.
	* @param Object definitionList.tuc[i].meanings.text		The definition from the word that has been searched on. 
	*/
	procesDefinition: function(definitionList) {
		var found = 0;
		if(definitionList.tuc != undefined) {			
			for(var i = 0; i < definitionList.tuc.length; i++) {
				if(found < 3 && definitionList.tuc[i].meanings != undefined){
					var def = (definitionList.tuc[i].meanings[0].text);
					definition.items.push(def);
					found++;
				}
			}
		}
		definition.output();
	},
	
	/**
	* Displaying general text/information on the panel.
	* @param Object panel	The panel div.
	* @param String	text	The text to be displayed.
	*/
	generalInfo: function(panel, text) {
		var titel = content.document.createElement("div");
		titel.appendChild(content.document.createTextNode(text));
		titel.id = 'piraat-titel';
		panel.appendChild(titel);
	},
	
	/**
	* Displaying the definitions on the panel, display error if there aren't definitions available.
	* Also saving the word that has been searched on, in the database.
	* @param String Woordenschat.url	The url to the host (usually 'localhost').
	*/
	output: function(){
		var def = '';
		if(definition.items.length > 0)
			def = definition.items[0];
		ajax(Woordenschat.url+'php/index.php?task=savePublicDefinition&word='+Woordenschat.word+'&definition='+def, function(){});
		var panel = content.document.getElementById('piraat-panel');
		var textbox = content.document.createElement("div");
   	 	textbox.id = 'piraat-textboxPanel'
		if(definition.items.length == 0){
			var error = document.createTextNode('Sorry er zijn helaas geen definities gevonden.');
			textbox.appendChild(error);
			panel.appendChild(textbox);
			return;
		}
		for(var i = 0; i<definition.items.length; i++){
			textbox.innerHTML += ' '+definition.items[i];
		}
		definition.generalInfo(panel, 'Zoek betekenis: ' + Woordenschat.word);
		panel.appendChild(textbox);
		definition.items = [];
	},
	
	/**
	 * Creating a textarea where the User can enter their own definition.
	 */
	userDefinition: function() {
		var panel = content.document.getElementById('piraat-panel');
		var textBox = content.document.createElement("textarea");
		textBox.cols = 45;
		textBox.rows = 7;
		textBox.id = 'piraat-userDefTextbox';
		definition.generalInfo(panel, 'Schrijf de betekenis in je eigen woorden');
		panel.appendChild(textBox);
	},
	
	/**
	 * Saving the definition that has been given by the User.
	 */
	saveUserDefinition: function() {
		var userDefinition = content.document.getElementById('piraat-userDefTextbox');
		ajax(Woordenschat.url + "php/index.php?task=saveDefinition&word="+ Woordenschat.word + "&definition=" + userDefinition.value, function(){
		});
	},
}

